package com.example.javademo;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.Arrays;
import java.util.List;

public class TabActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_tab);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        TabLayout tabLayout = findViewById(R.id.tabLayout);
        ViewPager2 viewPager = findViewById(R.id.viewPager);

        // 标签页标题
        List<String> tabTitles = Arrays.asList("首页", "发现", "我的");

        // 设置适配器TabFragment
        TabAdapter adapter = new TabAdapter( this,tabTitles);
        viewPager.setAdapter(adapter);

        // 绑定 TabLayout 和 ViewPager2

        new TabLayoutMediator(tabLayout, viewPager, (tab, position) -> {
            tab.setText(tabTitles.get(position));
        }).attach();

//        tabLayout.addTab(tabLayout.newTab().setText("Tab 1"));
//        tabLayout.addTab(tabLayout.newTab().setText("Tab 2"));
//        tabLayout.addTab(tabLayout.newTab().setText("Tab 3"));
        /* 设置切换效果 */
        /*tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            *//**
             * 当Tab被选中时触发的回调方法
             *
             * @param tab 被选中的Tab对象，包含Tab的相关信息和状态
             *//*
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                String text = tab.getText().toString();
            }
            *//**
             * 当TabLayout中的标签页取消选中时触发的回调方法
             *
             * @param tab 表示取消选中的标签页对象，包含标签页的相关信息和状态
             *//*
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }
            *//**
             * 当TabLayout中的标签页被重新选择时触发的回调方法
             *
             * @param tab 被重新选择的标签页对象，包含标签页的相关信息和状态
             *//*
            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });*/

    }
}